package com.xm.dao.reception_desk;

import com.xm.entity.*;
import com.xm.exception.SqlErrorException;
import com.xm.util.BaseUtil;
import org.intellij.lang.annotations.Language;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 用户账号信息表
 *
 * @author 康文程
 * @date 2023-12-25 上午 09:34
 */
public class AccountInformationDao extends BaseUtil {
    /**
     * 新增用户信息
     *
     * @param name      姓名
     * @param accountId 账号编号
     * @return true 新增成功  false 新增失败
     * @throws SqlErrorException SQL错误
     */
    public boolean addAccountInformation(String name, int accountId) throws SqlErrorException {
        @Language("sql")
        String sql = "INSERT INTO account_information(name, account_id) VALUES(?, ?)";
        int affectedRows = executeUpdate(sql, name, accountId);
        return affectedRows != 0;
    }
    /**
     * 根据账号编号查询用户名称和账户信息ID
     * @param accountId 账号编号
     * @return 用户信息对象
     * @throws SqlErrorException SQL错误
     */
    public AccountInformation getAccountInformationByAccountId(int accountId) throws SqlErrorException {
        @Language("sql")
        String sql = "SELECT information_id, name FROM account_information WHERE account_id = ?";
        ResultSet resultSet = executeQuery(sql, accountId);

        AccountInformation accountInformation = null;
        try {
            if (resultSet.next()) {
                accountInformation = new AccountInformation(
                        resultSet.getInt("information_id"),
                        resultSet.getString("name")
                );
            }
        } catch (SQLException e) {
            throw new SqlErrorException("查询失败",e);
        }finally {
            close(resultSet);
        }

        return accountInformation;
    }


    /**
     * 根据用户编号查询用户信息
     * @param informationId 用户编号
     * @return 用户信息对象
     * @throws SqlErrorException SQL错误
     */
    public AccountInformation getPersonalCenter(int informationId) throws SqlErrorException {
        @Language("SQL")
        String sql = "SELECT information_id,avatar,name,a.phone,a.mailbox FROM account_information ai" +
                " join account a on ai.account_id = a.account_id" +
                " WHERE information_id = ?";
        ResultSet resultSet = executeQuery(sql, informationId);

        AccountInformation accountInformation=null;
        try {
            if (resultSet.next()) {
                Account account = new Account(
                        resultSet.getString("phone"),
                        resultSet.getString("mailbox")
                );
                accountInformation = new AccountInformation(
                        resultSet.getInt("information_id"),
                        resultSet.getString("name"),
                        resultSet.getString("avatar"),
                        account
                );
            }
        } catch (SQLException e) {
            throw new SqlErrorException("查询失败",e);
        }finally {
            close(resultSet);
        }

        return accountInformation;
    }


    /**
     * 根据 account_id 查询账号信息Id
     * @param accountId 账号ID
     * @return AdministratorInformation 账号信息
     */
    public Integer queryByAccountId(Integer accountId) {
        @Language("sql")
        String sql = "SELECT information_id FROM account_information ai WHERE ai.account_id = ?";
        ResultSet rs = null;
        try {
            rs = executeQuery(sql, accountId);
            if (rs.next()) {
                return rs.getInt("information_id");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(rs);
        }
        return null;
    }
    /**
     * 根据信息ID查询姓名
     *
     * @param informationId 信息ID
     * @return 姓名
     * @throws SqlErrorException SQL错误
     */
    public String getNameByInformationId(int informationId) throws SqlErrorException {
        @Language("sql")
        String sql = "SELECT name FROM account_information WHERE information_id = ?";
        ResultSet resultSet = executeQuery(sql, informationId);
        String name = null;
        try {
            if (resultSet.next()) {
                name = resultSet.getString("name");
            }
        } catch (SQLException e) {
            throw new SqlErrorException("查询失败", e);
        } finally {
            close(resultSet);
        }
        return name;
    }

}
